﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;

namespace NGM.MicrosoftConnect {
    public class AuthorizationRequestBuilder {
        private readonly string _clientId;
        private readonly Uri _redirectUri;
        private readonly ResponseType _responseType;
        private List<Scope> _scopes = new List<Scope>();

        public AuthorizationRequestBuilder(string clientId, Uri redirectUri, ResponseType responseType) {
            _clientId = clientId;
            _redirectUri = redirectUri;
            _responseType = responseType;
        }

        public AuthorizationRequestBuilder(string clientId, Uri redirectUri, ResponseType responseType, IEnumerable<Scope> scopes) 
            : this(clientId, redirectUri, responseType){
            _scopes = scopes.ToList();
        }

        private Display _display = Display.Popup;
        private CultureInfo _locale = null;

        public AuthorizationRequestBuilder WithDisplay(Display display) {
            _display = display;
            return this;
        }

        public AuthorizationRequestBuilder WithLocale(CultureInfo locale) {
            _locale = locale;
            return this;
        }

        public AuthorizationRequestBuilder AddScope(Scope scope) {
            _scopes.Add(scope);
            return this;
        }

        public AuthorizationRequest Build() {
            var request = new AuthorizationRequest();
            request.ClientId = _clientId;
            request.RedirectUri = _redirectUri;
            request.ResponseType = _responseType;
            request.Scopes = _scopes;

            request.Display = _display;
            if (_locale != null)
                request.Locale = _locale;

            return request;
        }
    }
}